package com.huawei.admins.platform.gateway.core.component.predicate;

import org.reactivestreams.Publisher;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;

public class OrAsyncPredicate <T> implements AsyncPredicate<T> {

    private final AsyncPredicate<? super T> left;

    private final AsyncPredicate<? super T> right;

    public OrAsyncPredicate(AsyncPredicate<? super T> left, AsyncPredicate<? super T> right) {
        Assert.notNull(left, "Left AsyncPredicate must not be null");
        Assert.notNull(right, "Right AsyncPredicate must not be null");
        this.left = left;
        this.right = right;
    }

    @Override
    public Publisher<Boolean> apply(T t) {
        return Mono.from(left.apply(t)).flatMap(result -> result ? Mono.just(true) : Mono.from(right.apply(t)));
    }

    @Override
    public String toString() {
        return String.format("(%s || %s)", this.left, this.right);
    }

}
